package com.example.love_margin.Service.Impl;


import com.example.love_margin.Entity.Organization;
import com.example.love_margin.Entity.Until.PageInfo;
import com.example.love_margin.Mapper.OrganizationMapper;
import com.example.love_margin.Service.Impl.Until.delPetsUtil;
import com.example.love_margin.Service.OrganizationService;
import com.example.love_margin.Untils.ResultCode;
import com.example.love_margin.Untils.forDelete;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrganizationServiceImpl implements OrganizationService {
    @Autowired
    OrganizationMapper organizationMapper;
    //批量删除实例化
    @Autowired
    delPetsUtil delP = new delPetsUtil();

    //创建删除批量文件的实例化
    @Autowired
    forDelete delete = new forDelete();

    @Override
    public Object addOrganization(Organization organization) {
        if(organizationMapper.addOr(organization)==1){
            return ResultCode.SUCCESS.result("增加成功");
        }
        return ResultCode.SUCCESS.result("增加失败");
    }

    @Override
    public Object deleteOrganization(int organization_id) {
        if(organizationMapper.deleteOr(organization_id)==1){
            return ResultCode.SUCCESS.result("删除成功");
        }
        return ResultCode.SUCCESS.result("删除失败");
    }

    @Override
    public Object deleteOrganizations(Integer[] organization_ids) {
        String sql = delP.deleSQL("organization","organization_id",organization_ids);
        String URLs = delP.admin("organization_picture","organization","organization_id",organization_ids);
        List<String> realUrls = organizationMapper.selectPetAll(URLs);
        if(delete.DeleteList(realUrls)){
            if(organizationMapper.deleteOrs(sql)>=1){
                return ResultCode.SUCCESS.result( "删除成功");
            }else {
                return ResultCode.SUCCESS.result( "删除失败");
            }
        }
        return ResultCode.SUCCESS.result( "删除失败");
    }

    @Override
    public Object selectOrganization(int organization_id) {
        return ResultCode.SUCCESS.result(organizationMapper.selectOne(organization_id));
    }

    @Override
    public Object selectOrganizations(Integer pageNum, Integer pageSize) {
        //用插件进行分页
        PageHelper.startPage(pageNum, pageSize);
        List<Organization> user = organizationMapper.selectMany();
        // 需要把Page包装成PageInfo对象才能序列化。该插件也默认实现了一个PageInfo
        PageInfo<Organization> pageInfo = new PageInfo<>(user);
        return ResultCode.SUCCESS.result(pageInfo);
    }

    @Override
    public Object updateOrganization(Organization organization) {
        if(organizationMapper.updateOr(organization)==1){
            return ResultCode.SUCCESS.result("修改成功");
        }
        return ResultCode.SUCCESS.result("修改失败");
    }
}
